package com.truthbean.jutils.generate;

import com.truthbean.jutils.time.DateUtils;
import com.truthbean.jutils.file.FileUtils;
import com.truthbean.jutils.file.PropertiesUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;

/**
 * Created by TruthBean on 2016-08-31 11:13.
 */
public class CodeGenerator {
    public static void main(String[] args) throws Exception {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();

        Template servlet = ve.getTemplate("template/TemplateController.vm");

        String path = System.getProperty("user.dir").replace("\\", "/") + "/src/main";

        Properties properties = PropertiesUtils.readProperties(path + "/resources/generator.properties");

        VelocityContext ctx = new VelocityContext(properties);

        ctx.put("datetime", DateUtils.formatTime(new Date()));

        String classNameLowCase = properties.getProperty("className");
        ctx.put("classNameLowCase", properties.getProperty("className"));
        String classNameUpCase = classNameLowCase.substring(0, 1).toUpperCase() + classNameLowCase.substring(1);
        ctx.put("classNameUpCase", classNameUpCase);

        String[][] attrs = {
                {"Integer", "id"},
                {"String", "name"},
                {"String", "serializeNo"},
                {"String", "titile"},
                {"String", "subject"}
        };
        ctx.put("attrs", attrs);

        String businessPackagePath = path + "/java/" + properties.getProperty("package").replace(".", "/") + "/" + properties.getProperty("business");
        System.out.println(properties.getProperty("package").replace(".", "/"));

        String ctrlPkgPaht = businessPackagePath + "/controller/";
        merge(servlet, ctx, ctrlPkgPaht, ctrlPkgPaht + classNameUpCase + "Controller.java");
//        merge(listJspTpt, ctx, rootPath + "/webapp/teacherList.jsp");
//        merge(addTpt, ctx, rootPath + "/webapp/teacherAdd.jsp");
//        merge(modifyTpt, ctx, rootPath + "/webapp/teacherModify.jsp");
        System.out.println("success...");
    }

    private static void merge(Template template, VelocityContext ctx, String _package, String javaPath) {
        System.out.println(_package);
        File dir = new File(_package);
        if (!dir.isDirectory()) {
            System.out.println("目录不存在，即将创建...");
            dir.mkdirs();
        }
        try (PrintWriter writer = new PrintWriter(javaPath)) {
            template.merge(ctx, writer);
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
